home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #2 / Amiga Plus CD - 1999 - No. 2.iso / System-Boost / Workbench / Archive / XPK / xpk_Source / test / testSeek.c < prev    next >
C/C++ Source or Header  |  1998-11-15  |  4KB  |  192 lines

  1. #define NAME        "testSeek"
  2. #define DISTRIBUTION    "(Freeware) "
  3. #define REVISION    "0"
  4.  
  5. /* Programmheader
  6.  
  7.     Name:        testSeek
  8.     Author:        SDI
  9.     Distribution:    Freeware
  10.     Description:    tests seeking in xpk files
  11.     Compileropts:    -
  12.     Linkeropts:    -l xpkmaster amiga
  13.  
  14.  1.0   30.10.98 : first version
  15. */
  16.  
  17. /* For correct working you need to use at least xpkmaster.library 5! */
  18.  
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21. #include <proto/xpkmaster.h>
  22. #include <exec/memory.h>
  23. #include <exec/execbase.h>
  24. #include "SDI_defines.h"
  25.  
  26. struct Library        *XpkBase        = 0;
  27. ULONG            DosVersion        = 37;
  28.  
  29. #define PARAM "FROM/A"
  30.  
  31. struct Args {
  32.   STRPTR from;
  33. };
  34.  
  35. struct BufData {
  36.   STRPTR       buffer;
  37.   ULONG         start;
  38.   ULONG         filled;
  39.   LONG         err;
  40.   struct XpkFib *xfib;
  41. };
  42.  
  43. LONG SeekData(struct BufData *b, LONG size);
  44. LONG ReadData(struct BufData *b, APTR buf, ULONG size);
  45.  
  46. void main(void)
  47. {
  48.   struct Args args;
  49.   struct RDArgs *rda;
  50.  
  51.   if((rda = ReadArgs(PARAM, (LONG *) &args, 0)))
  52.   {
  53.     if((XpkBase = OpenLibrary(XPKNAME, 5)))
  54.     {
  55.       struct BufData b;
  56.       LONG err;
  57.  
  58.       if(!(err = XpkOpenTags(&b.xfib, XPK_InName, args.from, XPK_NeedSeek,
  59.       TRUE, XPK_PassThru, TRUE, TAG_DONE)))
  60.       {
  61.         if((b.buffer = (STRPTR) AllocVec(b.xfib->xf_NLen, MEMF_ANY)))
  62.         {
  63.           b.start = 0;
  64.           b.filled = 0;
  65.           b.err    = 0;
  66.  
  67.       /* this is a test, loading useless stuff saved with textChunkyPack */
  68.       {
  69.         ULONG buf[50];
  70.  
  71.         /* because of delayed error, we need not test all calls! */
  72.         if((err = ReadData(&b, buf, 8)))
  73.           XpkPrintFault(err, 0);
  74.         else if(buf[0] == 0x49445854 && buf[1] == 0x43503130)
  75.         {
  76.           Printf("ID value IDXTCP10 is correct\n");
  77.  
  78.           if((err = ReadData(&b, buf, 21)))
  79.             XpkPrintFault(err, 0);
  80.           else
  81.           {
  82.             Printf("%s\n", buf);    /* dosbase text */
  83.             SeekData(&b, sizeof(struct DosLibrary));
  84.             if((err = ReadData(&b, buf, 21)))
  85.               XpkPrintFault(err, 0);
  86.             else
  87.             {
  88.               Printf("%s\n", buf); /* sysbase text */
  89.               SeekData(&b, sizeof(struct ExecBase));
  90.               if((err = ReadData(&b, buf, 21)))
  91.                 XpkPrintFault(err, 0);
  92.               else
  93.               {
  94.                 ULONG a = *((ULONG *)(((STRPTR) 0x1000000)-20));
  95.                 Printf("%s\n", buf); /* kickstart text */
  96.                 if((err = SeekData(&b, a-20)))
  97.                   XpkPrintFault(err, 0);
  98.                 else
  99.                 {
  100.                   if((err = ReadData(&b, buf, 4)))
  101.                     XpkPrintFault(err, 0);
  102.                   else
  103.                     Printf("Kickstart size is correct: %ld\n", buf[0]);
  104.                   Printf("Now read too much and produce an error\n");
  105.                   if((err = ReadData(&b, 0, 10000)))
  106.                     XpkPrintFault(err, 0);
  107.                   Printf("Now seek too much and produce an error\n");
  108.                   if((err = SeekData(&b, 10000)))
  109.                     XpkPrintFault(err, 0);
  110.                 }
  111.               }
  112.             }
  113.           }
  114.         }
  115.         else
  116.           Printf("Wrong ID\n");
  117.       }
  118.           
  119.           FreeVec(b.buffer);
  120.         }
  121.         XpkClose(b.xfib);
  122.       }
  123.       else
  124.         XpkPrintFault(err, 0);
  125.       CloseLibrary(XpkBase);
  126.     }
  127.     FreeArgs(rda);
  128.   }
  129. }
  130.  
  131. /* Call this function for seeking in stored data */
  132. LONG SeekData(struct BufData *b, LONG size)
  133. {
  134.   LONG i;
  135.   
  136.   if((i = XpkSeek(b->xfib, size - b->filled + b->start, XPKSEEK_CURRENT)) < 0)
  137.     b->err = i;
  138.   else
  139.   {
  140.     b->start = i;
  141.     if((i = XpkRead(b->xfib, b->buffer, b->xfib->xf_NLen)) < 0)
  142.       b->err = i;
  143.     else
  144.       b->filled = i;
  145.   }
  146.  
  147.   return b->err;
  148. }
  149.  
  150. /* Call this function for reading stored data, calling it with buffer zero
  151. means skipping some data.
  152.  
  153. This function uses a delayed error report, so we need not check every call.
  154. ReadData does nothing except returning the error again, when an error
  155. occured a call before. */
  156. LONG ReadData(struct BufData *b, APTR buf, ULONG size)
  157. {
  158.   LONG i;
  159.  
  160.   while(size && !b->err)
  161.   {
  162.     if(b->filled == b->start)
  163.     {
  164.       if((i = XpkRead(b->xfib, b->buffer, b->xfib->xf_NLen)) < 0)
  165.         b->err = i;
  166.       else
  167.       {
  168.     if(!i)    /* we reached the end */
  169.       b->err = XPKERR_IOERRIN;
  170.         b->filled = i;
  171.         b->start = 0;
  172.       }
  173.     }
  174.     if(!b->err)
  175.     {
  176.       if((i = b->filled - b->start) > size)
  177.         i = size;
  178.  
  179.       if(buf)
  180.       {
  181.         CopyMem(b->buffer+b->start, buf, i);
  182.         buf = ((STRPTR) buf) + i;
  183.       }
  184.       b->start += i;
  185.       size -= i;
  186.     }
  187.   }
  188.  
  189.   return b->err;
  190. }
  191.  
  192.